import $service from '../../../../service'
const app = getApp();
Page({
  data: {
    questionData: {},
    answerList: {},
    num: 0,
    answers: []
  },
  onLoad() {
    let questionData = JSON.parse($service.getStore('questionnaire'))
    this.setData({
      questionData,
      answerList: questionData.options
    })    
  },
  selectAnwser(e) {
    let {index, con} = e.currentTarget.dataset
    this.data.answers[this.data.num] = con
    if (this.data.num  == this.data.answerList.length - 1) { // 如果最后一题先把选中的去掉在选择答案
      this.data.answerList.forEach(item => {
        item.isChange = false
      })
    }
    // 先选中答案
    this.data.answerList[index].isChange = true
    this.setData({
      answerList: this.data.answerList,
      answers: this.data.answers
    })
    // 再跳到一下道问题
    if (this.data.num < this.data.answerList.length - 1) {
      this.data.answerList.forEach(item => {
        item.isChange = false
      })
      this.data.num ++
      setTimeout(() => {
        this.setData({
          answerList: this.data.answerList,
          num: this.data.num
        })
      },200)
    }
  },
  sumit() {
    wx.showLoading({
      title: '报告生成中...',
    })
    if (this.data.answers.length < 5) {
      $service.showToast('请完成问卷再进行提交')
      return
    } 
    let params = {
      "answers": this.data.answers.join('|'),
      "mainCorporeity": this.data.questionData.mainCorporeity,
      "requestId": this.data.questionData.requestId
    }
    app.http.corporeityResult(params).then(res => {
      wx.redirectTo({
        url: '../toning_solutions/index?requestId=' + res.data.requestId
      })
    }).catch(err => {
      $service.showToast(err.msg)
    }).finally(() => {
      wx.hideLoading()
    })
  }
})